﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IWMAG.Core.EnhancedProperties
{
    public interface IPropertyContainer
    {
        UniqueIdentifier UniqueID { get; }
    }

    //TODO: (sanjay) experimental
    class GameEvent<T> : IObservable<T>
    {
        public GameEvent(out EventController i)
        {
            i = new EventController(this);
            
        }
        public class EventController
        {
            GameEvent<T> t;
            public EventController(GameEvent<T> t)
            {
                this.t = t;
            }

            public void SendNext(T val)
            {
                foreach (var item in t.observers)
                {
                    item.OnNext(val);
                }
            }

            public void SendCompletion()
            {
                foreach (var item in t.observers)
                {
                    item.OnCompleted();
                }
            }

            public void SendError(Exception e)
            {
                foreach (var item in t.observers)
                {
                    item.OnError(e);
                }
            }
        }

        private List<IObserver<T>> observers;

        public IDisposable Subscribe(IObserver<T> observer)
        {
            observers.Add(observer);
            return new ObserverDisposable(this, observer);
        }

        private class ObserverDisposable : IDisposable
        {
            private GameEvent<T> intern;
            private IObserver<T> obs;

            

            public ObserverDisposable(GameEvent<T> gameEvent, IObserver<T> observer)
            {
                this.intern = gameEvent;
                this.obs = observer;
            }
            public void Dispose()
            {
                intern.observers.Remove(obs);
            }
        }
    }
}
